package com.google.code.meitt.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.code.meitt.model.Game;

public class DB {
	private final static String username = "root";
	private final static String password = "0pHXwuYrW6";

	private static Connection createConnection() {
		Connection connection = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(
					"jdbc:mysql://mysql-meitt.jelastic.dogado.eu/meitt",
					username, password);
		} catch (SQLException ex) {
			try {
				connection = DriverManager.getConnection(
						"jdbc:mysql://localhost/meitt", "meitt", "parool");
			} catch (SQLException e) {
				ex.printStackTrace();
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return connection;
	}

	public static boolean runStatement(String stmt) {
		boolean result = false;
		Connection connection = null;
		try {
			connection = createConnection();
			Statement statement = connection.createStatement();
			result = statement.execute(stmt);
			statement.close();
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (connection != null){
					connection.close();
				}
			} catch (SQLException e) {
			}
		}
		return result;
	}

	public static int runInsert(String stmt) {
		int result = 0;
		Connection connection = null;
		try {
			connection = createConnection();
			PreparedStatement stm = connection.prepareStatement(stmt,
					Statement.RETURN_GENERATED_KEYS);
			stm.execute();
			ResultSet generatedKeys = stm.getGeneratedKeys();
			if (generatedKeys.next()) {
				result = generatedKeys.getInt(1);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally{
			try {
				if(connection!=null){
					connection.close();
				}
			} catch (SQLException e) {
			}
		}
		return result;
	}

	public static List<Map<String, Object>> runQuery(String query) {
		List<Map<String,Object>> results = new ArrayList<Map<String,Object>>();
		ResultSet rs = null;
		Connection connection = null;
		try {
			connection = createConnection();
			Statement statement = connection.createStatement();
			rs = statement.executeQuery(query);
			if(rs.next()){
				do{
					Map<String,Object> map = new HashMap<String,Object>();
					ResultSetMetaData metaData = rs.getMetaData();
					int columnCount = metaData.getColumnCount();
					for(int i = 1;i<=columnCount;i++){
						map.put(metaData.getColumnName(i), rs.getObject(i));
					}
					results.add(map);
				}while(rs.next());
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(connection != null){
					connection.close();
				}
			} catch (SQLException e) {
			}
		}
		return results;
	}

	public static boolean runUpdate(String update) {
		int result = 0;
		try {
			Statement statement = createConnection().createStatement();
			result = statement.executeUpdate(update);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// returns false if no updates made
		return result != 0;
	}

}
